import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/HomeIndex'
import Login from '@/views/LoginIndex'
import Echarts from '@/views/SecondLevel/EchartsHome'
import ProductClass from '@/views/SecondLevel/ProductClass'
import ProductManagement from '@/views/SecondLevel/ProductManagement'

import store from '@/store'

// 人事管理
import ListView from '@/views/Personnel/ListView'
import Statistics from '@/views/Personnel/Statistics'
import Workload from '@/views/Personnel/Workload'
import District from '@/views/SecondLevel/DistrictManagement'
import PointManagement from '@/views/SecondLevel/PointManagement'
import PartnerManagement from '@/views/SecondLevel/PartnerManagement'
import OrderManagement from '@/views/SecondLevel/OrderManagement'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Home,
    redirect: '/echarts',
    children: [
      {
        path: '/echarts',
        component: Echarts
      },
      {
        path: '/skuclass',
        component: ProductClass
      },
      {
        path: '/sku',
        component: ProductManagement
      },
      {
        path: '/4-1',
        component: ListView
      },
      {
        path: '/4-2',
        component: Statistics
      },
      {
        path: '/4-3',
        component: Workload
      },
      {
        path: '/district',
        component: District
      },
      {
        path: '/point',
        component: PointManagement
      },
      {
        path: '/partner',
        component: PartnerManagement
      },
      {
        path: '/order',
        component: OrderManagement
      },
      {
        path: '/policy',
        component: () => import('@/views/SecondLevel/PolicyManagement')
      }
    ]
  },
  {
    path: '/login',
    component: Login
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach(async (to, from, next) => {
  const { token } = await store.state.user
  if (to.path !== '/login' && !token) return next('/login')
  next()
})

export default router
